<%@page pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>

<script>
    $(function () {
        pageInit();
    });

    function pageInit() {
        jQuery("#userTable").jqGrid(
            {
                url: "${path}/user/queryAllPage",
                editurl: "${path}/user/edit",
                datatype: "json",
                mtype: "post",
                rowNum: 10,
                rowList: [10, 20, 30],
                pager: '#userPage',
                sortname: 'id',
                sortorder: "desc",
                viewrecords: true,  //是否展示总条数
                caption: "用户信息",
                styleUI: "Bootstrap",
                autowidth: true,
                height: "auto",
                colNames: ['Id', '手机号', '用户名', '头像', '签名', '状态', '注册时间', '微信'],
                colModel: [
                    {name: 'id', width: 55, align: "center"},
                    {name: 'phone', editable: true, width: 90, align: "center",},
                    {name: 'username', editable: true, width: 100, align: "center"},
                    {
                        name: 'headImg', editable: true, edittype: "file", align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            return "<img src='" + cellvalue + "'>";
                        }
                    },
                    {name: 'sign', editable: true, align: "center"},
                    {
                        name: 'status', width: 80, align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue == 1) {
                                //1正常
                                return "<button class='btn btn-info' onclick='updateStatus(\"" + rowObject.id + "\",\"" + rowObject.status + "\")'>正常</button>";
                            } else {
                                return "<button class='btn btn-danger' onclick='updateStatus(\"" + rowObject.id + "\",\"" + rowObject.status + "\")'>冻结</button>";
                            }
                        }
                    },
                    {name: 'registTime', align: "center"},
                    {name: 'wechat', sortable: false}
                ]
            });
        $("#userTable").jqGrid('navGrid', '#userPage', {edit: true, add: true, del: true},
            {
                closeAfterEdit: true,
            },
            {
                closeAfterAdd: true,
                afterSubmit: function (data) {
                    //console.log(data)
                    //文件异步上传
                   $.ajaxFileUpload({
                        url: "${path}/user/fileUpload",
                        type: "post",
                        dataType: "json",
                        data: {"userId":data.responseText},  //返回的id
                        fileElementId:"headImg",
                        success: function (data) {
                            $("#userTable").trigger("reloadGrid");
                        }
                    });
                        return "hello";
                }
            },
            {
                afterSubmit: function (data) {

                }
            }
        );
    }

    function updateStatus(id, status) {
        /*alert("updateActivce,"+"id:"+id+","+"status:"+status);*/
        if (status == 1) {
            $.get("${path}/user/updateStatus", {"id": id, "status": "0"}, function () {
                //refresh
                $("#userTable").trigger("reloadGrid");
            });
        } else {
            $.get("${path}/user/updateStatus", {"id": id, "status": "1"}, function () {
                //refresh
                $("#userTable").trigger("reloadGrid");
            });
        }


    }
</script>

<div class="panel panel-info">
    <div class="panel panel-heading">用户信息</div>

    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab"
                                                  data-toggle="tab">用户信息</a></li>
    </ul>

    <button class="btn btn-info">导出用户信息</button>&emsp;&emsp;&emsp;&emsp;
    <button class="btn btn-success">导出用户信息</button>&emsp;&emsp;&emsp;&emsp;
    <button class="btn btn-warning">导出用户信息</button>&emsp;&emsp;&emsp;&emsp;
    <button class="btn btn-danger">导出用户信息</button>&emsp;&emsp;&emsp;&emsp;
    <table id="userTable"></table>
    <div id="userPage">
    </div>
</div>
